import React from 'react'
// ===================================================================== Temp
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import Confirm from '#cpt/temp/Confirm'
import SearchForm from '#cpt/temp/SearchForm'
// import AddEditModal from '#cpt/temp/AddEditModal'
// ===================================================================== Declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Box 		= $Lazy.load(()=>import('#tp/box/box'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
const AddModal 	= $Lazy.state(()=>import('./cpt/add-modal'))
// ===================================================================== data
const KindStatus = [
	{ name: '开启', 	value: 2 },
	{ name: '草稿', 	value: 0 },
	{ name: '未开启', 	value: -1 },
	{ name: '审核',		value: 1 },
	{ name: '预添加',	value: -2}
]
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		// 搜索表单列表
		forms : [
			{ label:'关键字搜索',	name: 'keyword',  width:300	}
		],
		// 提交表单列表
		formData : [
			{ label: '关键词',	name: 'kind_rel_id', full:true, required:true,  type:'select' }
		],
	}
	
	model = {}
	
	componentDidMount(){
		const { kind_id } = this.props
		this.query = this.submit = { kind_id }
		this.fetch()
	}
	
	// 获取弹框下拉
	getModalSelect = async () => {
		const { formData } = this.state
		$fn.setModalRows(formData, [ 'kind_rel_id' ], ()=>{
			$cache.then( async f => {
				// 获取设备名称
				$fn.setRow( formData, 'kind_rel_id', await f.getCache({ api: 'nature_project' }))
				//
				this.setState({ formData })
			})
		})
	}
	
	// paging
	fetch = () => {
		let { api } = this.props
		api = api ? api : 'kd-market-rel/index'
		$fn.fetch.call(this, api, { param: {page:1, limit:1000}, callback:({sum_price})=>{ this.setState({sum_price}) } })
	}
	
	// 表单
	cols = [
		{ title: '项目代码', 		dataIndex: 'kind_code', 			width:150 },
		{ title: '项目名称', 		dataIndex: 'kind_name', 			width:150 },
		{ title: '简称', 			dataIndex: 'short_name', 			width:150 },
		{ title: '检测方法', 		dataIndex: 'check_method_name', 	width:150 },
		{ title: '项目属性', 		dataIndex: 'kind_type_name', 		width:150 },
		{ title: '价格', 			dataIndex: 'total_price', 			width:150 },
		{ title: '状态', 			dataIndex: 'kind_status', 			width:100,  render: text => $fn.getState(KindStatus, text) },
	]
	// 操作按钮
	controls = () => {
		const { disabled, delApi } = this.props
		return [
			// Controls.add.call(this,{click:this.getModalSelect, code:false, disabled}),
			Controls.click.call(this,'添加', ()=> this.modalRef.open(this.props.kind_id), { disabled } ),
			Confirm.call(this,'删除', delApi ? delApi : 'kd-market-rel/delete', { isKeys: true, ghost:true,disabled }),
		]
	}
	render(){
		const title = '项目明细'
		const { disabled, addApi, type } = this.props
		return (
			<>
				<Box title={title} controls={this.controls()} className='mt5'>
					{/* 搜索 */}
					{ SearchForm.call(this, { noRefresh:true, code:false, disabled }) }
					{/* 表格 */}
					{ 
						Table.call(this, { 
							wrapStyle: {height:300, padding:'10px 0 10px'},
							disabled,
							pag: false,
							suffix: <div className='fxm'><span>总计价格 :</span><b className='c0 f13 ml5 rel' style={{top:1}}>{this.state.sum_price||0}</b></div>
						})
					}
				</Box>
				{/* 添加 */}
				
				{/* 弹窗 */}
				<AddModal onRef={ref => this.modalRef = ref} onRefresh={this.fetch} api={addApi} type={type} />
				{/* { AddEditModal.call(this,{ title, labelWidth:'s5', width:500, api: addApi ? addApi : 'kd-market-rel/add'}) } */}
			</>
		)
	}
}

export default Index